home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / Sources / FWFont.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  13.1 KB  |  465 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWFont.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWFONT_H
  13. #include "FWFont.h"
  14. #endif
  15.  
  16. #ifndef FWGC_H
  17. #include "FWGC.h"
  18. #endif
  19.  
  20. #ifndef FWPOINT_H
  21. #include "FWPoint.h"
  22. #endif
  23.  
  24. #ifndef FWFXMATH_H
  25. #include "FWFxMath.h"
  26. #endif
  27.  
  28. #ifndef FWGRUTIL_H
  29. #include "FWGrUtil.h"
  30. #endif
  31.  
  32. #ifndef FWRASTER_H
  33. #include "FWRaster.h"
  34. #endif
  35.  
  36. #ifndef FWGDEV_H
  37. #include "FWGDev.h"
  38. #endif
  39.  
  40. // ----- Foundation Includes -----
  41.  
  42. #ifndef FWSTREAM_H
  43. #include "FWStream.h"
  44. #endif
  45.  
  46. #ifndef FWCOMMON_H
  47. #include "FWCommon.h"
  48. #endif
  49.  
  50. // ----- OpenDoc Includes -----
  51.  
  52. #ifndef _TRANSFORM_
  53. #include <Trnsform.xh>
  54. #endif
  55.  
  56. // ----- Macintosh Includes -----
  57.  
  58. #if defined(FW_BUILD_MAC) && !defined(__TEXTUTILS__)
  59. #include <TextUtils.h>
  60. #endif
  61.  
  62. #if defined(FW_BUILD_MAC) && !defined(__FONTS__)
  63. #include <Fonts.h>
  64. #endif
  65.  
  66. //========================================================================================
  67. //    RunTime Info
  68. //========================================================================================
  69.  
  70. #if FW_LIB_EXPORT_PRAGMAS
  71. #pragma lib_export on
  72. #endif
  73.  
  74. #ifdef FW_BUILD_MAC
  75. #pragma segment fwgraphx
  76. #endif
  77.  
  78. FW_DEFINE_CLASS_M1(FW_PFont, FW_CGraphicCountedPtr)
  79. FW_DEFINE_CLASS_M1(FW_CFontRep, FW_CGraphicCountedPtrRep)
  80.  
  81. FW_REGISTER_ARCHIVABLE_CLASS(FW_LFontRep, FW_CFontRep, FW_CFontRep::Read, FW_CGraphicCountedPtrRep::Write)
  82.  
  83. //========================================================================================
  84. //    struct FW_SFontMetrics
  85. //========================================================================================
  86.  
  87. //----------------------------------------------------------------------------------------
  88. //    FW_SFontMetrics::PrivGetFontMetrics
  89. //----------------------------------------------------------------------------------------
  90.  
  91. void FW_SFontMetrics::PrivGetFontMetrics(FW_CGraphicDevice* device, FW_SFontMetrics& fontMetrics)
  92. {    
  93. #ifdef FW_BUILD_MAC
  94. FW_UNUSED(device);
  95.     ::GetFontInfo(&fontMetrics);
  96. #endif
  97.  
  98. #ifdef FW_BUILD_WIN
  99.     HDC dc = device->GetPlatformCanvas();
  100.     ::SetMapMode(dc, MM_ANISOTROPIC);
  101.     
  102.     SIZE sizeOldWinExt;
  103.     ::SetWindowExtEx(dc, 1440, 1440, &sizeOldWinExt);
  104.     
  105.     SIZE sizeOldViewExt;
  106.     ::SetViewportExtEx(dc, ::GetDeviceCaps(dc, LOGPIXELSX), ::GetDeviceCaps(dc, LOGPIXELSY), &sizeOldViewExt);
  107.     
  108.     ::GetTextMetrics(dc, &fontMetrics);
  109.     
  110.     ::SetMapMode(dc, MM_TEXT);
  111.     ::SetWindowExtEx(dc, sizeOldWinExt.cx, sizeOldWinExt.cx, NULL);
  112.     ::SetViewportExtEx(dc, sizeOldViewExt.cx, sizeOldViewExt.cy, NULL);
  113. #endif
  114. }
  115.  
  116. //========================================================================================
  117. //    class FW_PFont
  118. //========================================================================================
  119.  
  120. //----------------------------------------------------------------------------------------
  121. //    FW_PFont::FW_PFont
  122. //----------------------------------------------------------------------------------------
  123.  
  124. FW_PFont::FW_PFont() :
  125.     FW_CGraphicCountedPtr()
  126. {
  127. }
  128.  
  129. //----------------------------------------------------------------------------------------
  130. //    FW_PFont::FW_PFont
  131. //----------------------------------------------------------------------------------------
  132.  
  133. FW_PFont::FW_PFont(const FW_CString& fontName, FW_FontStyle fontStyle, FW_CFixed fontSize) :
  134.     FW_CGraphicCountedPtr()
  135. {
  136.     SetRep(new FW_CFontRep(fontName, fontStyle, fontSize));
  137. }
  138.  
  139. //----------------------------------------------------------------------------------------
  140. //    FW_PFont::FW_PFont
  141. //----------------------------------------------------------------------------------------
  142.  
  143. FW_PFont::FW_PFont(const FW_PFont& other) :
  144.     FW_CGraphicCountedPtr(other)
  145. {
  146. }
  147.  
  148. //----------------------------------------------------------------------------------------
  149. //    FW_PFont::FW_PFont
  150. //----------------------------------------------------------------------------------------
  151.  
  152. FW_PFont::FW_PFont(FW_CFontRep* fontRep) :
  153.     FW_CGraphicCountedPtr()
  154. {
  155.     SetRep(fontRep);
  156. }
  157.  
  158. //----------------------------------------------------------------------------------------
  159. //    FW_PFont::FW_PFont
  160. //----------------------------------------------------------------------------------------
  161.  
  162. FW_PFont::~FW_PFont()
  163. {
  164. }
  165.  
  166. //----------------------------------------------------------------------------------------
  167. //    FW_PFont::operator=
  168. //----------------------------------------------------------------------------------------
  169.  
  170. FW_PFont& FW_PFont::operator=(const FW_PFont& other)
  171. {
  172.     // We don't need to test this == &other because SetRep will do it
  173.     SetRep(other.GetRep());
  174.     return *this;
  175. }
  176.  
  177. //----------------------------------------------------------------------------------------
  178. //    FW_PFont::operator=
  179. //----------------------------------------------------------------------------------------
  180.  
  181. FW_PFont& FW_PFont::operator=(FW_CFontRep* fontRep)
  182. {
  183.     // We don't need to test this == &other because SetRep will do it
  184.     SetRep(fontRep);
  185.     return *this;
  186. }
  187.  
  188. //----------------------------------------------------------------------------------------
  189. //    FW_PFont::FW_PFont
  190. //----------------------------------------------------------------------------------------
  191.  
  192. FW_PFont::FW_PFont(FW_EStandardFonts /* std */)
  193. {
  194.     SetRep(new FW_CFontRep(FW_kSystemFont, FW_kPlain, FW_IntToFixed(12)));
  195. }
  196.  
  197. //----------------------------------------------------------------------------------------
  198. //    FW_PFont::operator=
  199. //----------------------------------------------------------------------------------------
  200.  
  201. FW_PFont& FW_PFont::operator=(FW_EStandardFonts /* std */)
  202. {
  203.     SetRep(new FW_CFontRep(FW_kSystemFont, FW_kPlain, FW_IntToFixed(12)));
  204.     return *this;
  205. }
  206.  
  207. //========================================================================================
  208. //    class FW_CFontRep
  209. //========================================================================================
  210.  
  211. //----------------------------------------------------------------------------------------
  212. //    FW_CFontRep::FW_CFontRep
  213. //----------------------------------------------------------------------------------------
  214.  
  215. FW_CFontRep::FW_CFontRep() :
  216.     FW_CGraphicCountedPtrRep(),
  217.     fFontStyle(FW_kPlain),
  218.     fFontSize(FW_IntToFixed(12)),
  219.     fFontName()
  220. #ifdef FW_BUILD_MAC
  221.     ,fMacValidCache(FALSE)
  222. #endif
  223. {
  224. }
  225.  
  226. //----------------------------------------------------------------------------------------
  227. //    FW_CFontRep::FW_CFontRep
  228. //----------------------------------------------------------------------------------------
  229.  
  230. FW_CFontRep::FW_CFontRep(const FW_CString& fontName, FW_FontStyle fontStyle, FW_CFixed fontSize) :
  231.     FW_CGraphicCountedPtrRep(),
  232.     fFontStyle(fontStyle),
  233.     fFontSize(fontSize),
  234.     fFontName(fontName)
  235. #ifdef FW_BUILD_MAC
  236.     ,fMacValidCache(FALSE)
  237. #endif
  238. {
  239. }
  240.  
  241. //----------------------------------------------------------------------------------------
  242. //    FW_CFontRep::FW_CFontRep
  243. //----------------------------------------------------------------------------------------
  244.  
  245. FW_CFontRep::FW_CFontRep(const FW_CFontRep& otherRep):
  246.     FW_CGraphicCountedPtrRep(),
  247.     fFontStyle(otherRep.fFontStyle),
  248.     fFontSize(otherRep.fFontSize),
  249.     fFontName(otherRep.fFontName)
  250. #ifdef FW_BUILD_MAC
  251.     ,fMacValidCache(FALSE)
  252. #endif
  253. {
  254. }
  255.  
  256. //----------------------------------------------------------------------------------------
  257. //    FW_CFontRep::FW_CFontRep
  258. //----------------------------------------------------------------------------------------
  259.  
  260. FW_CFontRep::FW_CFontRep(FW_CReadableStream& archive) :
  261.     FW_CGraphicCountedPtrRep()
  262. #ifdef FW_BUILD_MAC
  263.     ,fMacValidCache(FALSE)
  264. #endif
  265. {
  266.     FW_CStringArchiver::Read(archive, fFontName);
  267.     
  268.     archive >> fFontStyle;
  269.     archive >> fFontSize;
  270. }
  271.  
  272. //----------------------------------------------------------------------------------------
  273. //    FW_CFontRep::~FW_CFontRep
  274. //----------------------------------------------------------------------------------------
  275.  
  276. FW_CFontRep::~FW_CFontRep()
  277. {
  278. }
  279.  
  280. //----------------------------------------------------------------------------------------
  281. //    FW_CFontRep::FW_CFontRep
  282. //----------------------------------------------------------------------------------------
  283.  
  284. FW_CFontRep& FW_CFontRep::operator=(const FW_CFontRep& otherRep)
  285. {
  286.     fFontName = otherRep.fFontName;
  287.     fFontStyle = otherRep.fFontStyle;
  288.     fFontSize = otherRep.fFontSize;
  289.  
  290. #ifdef FW_BUILD_MAC
  291.     fMacValidCache = FALSE;
  292. #endif
  293.     return *this;
  294. }
  295.  
  296. //----------------------------------------------------------------------------------------
  297. //    FW_CFontRep::Copy
  298. //----------------------------------------------------------------------------------------
  299.  
  300. FW_PFont FW_CFontRep::Copy() const
  301. {
  302.     FW_PFont font(fFontName, fFontStyle, fFontSize);
  303.     return font;
  304. }
  305.  
  306. //----------------------------------------------------------------------------------------
  307. //    FW_CFontRep::SetFontName
  308. //----------------------------------------------------------------------------------------
  309.  
  310. void FW_CFontRep::SetFontName(const FW_CString& fontName)
  311. {
  312.     fFontName = fontName;
  313. #ifdef FW_BUILD_MAC
  314.     fMacValidCache = FALSE;
  315. #endif
  316. }
  317.  
  318. //----------------------------------------------------------------------------------------
  319. //    FW_CFontRep::SetFontStyle
  320. //----------------------------------------------------------------------------------------
  321.  
  322. void FW_CFontRep::SetFontStyle(FW_FontStyle fontStyle)
  323. {
  324.     fFontStyle = fontStyle;
  325. #ifdef FW_BUILD_MAC
  326.     fMacValidCache = FALSE;
  327. #endif
  328. }
  329.  
  330. //----------------------------------------------------------------------------------------
  331. //    FW_CFontRep::Flatten
  332. //----------------------------------------------------------------------------------------
  333.  
  334. void FW_CFontRep::Flatten(FW_CWritableStream& archive) const
  335. {
  336.     FW_CStringArchiver::Write(archive, fFontName);
  337.  
  338.     archive << fFontStyle;
  339.     archive << fFontSize;
  340. }
  341.  
  342. #ifdef FW_BUILD_MAC
  343. //----------------------------------------------------------------------------------------
  344. //    FW_CFontRep::MacGetFontID
  345. //----------------------------------------------------------------------------------------
  346.  
  347. short FW_CFontRep::MacGetFontID() const
  348. {
  349.     ((FW_CFontRep*)this)->MacCheckFontCache();
  350.     return fMacFontID;
  351. }
  352. #endif
  353.  
  354. #ifdef FW_BUILD_MAC
  355. //----------------------------------------------------------------------------------------
  356. //    FW_CFontRep::MacGetFontStyle
  357. //----------------------------------------------------------------------------------------
  358.  
  359. Style FW_CFontRep::MacGetFontStyle() const
  360. {
  361.     ((FW_CFontRep*)this)->MacCheckFontCache();
  362.     return fMacFontStyle;
  363. }
  364. #endif
  365.  
  366. #ifdef FW_BUILD_MAC
  367. //----------------------------------------------------------------------------------------
  368. //    FW_CFontRep::MacCheckFontCache
  369. //----------------------------------------------------------------------------------------
  370.  
  371. void FW_CFontRep::MacCheckFontCache()
  372. {
  373.     if (!fMacValidCache)
  374.     {
  375.         fMacFontStyle = normal;
  376.         if (fFontStyle & FW_kBold)
  377.             fMacFontStyle += bold;
  378.         if (fFontStyle & FW_kItalic)
  379.             fMacFontStyle += italic;
  380.         if (fFontStyle & FW_kUnderline)
  381.             fMacFontStyle += underline;
  382.         if (fFontStyle & FW_kStrikeOut)
  383.             fMacFontStyle += normal;
  384.         if (fFontStyle & FW_kOutline)
  385.             fMacFontStyle += outline;
  386.         if (fFontStyle & FW_kShadow)
  387.             fMacFontStyle += shadow;
  388.         if (fFontStyle & FW_kExtended)
  389.             fMacFontStyle += extend;
  390.         if (fFontStyle & FW_kCondensed)
  391.             fMacFontStyle += condense;
  392.                     
  393.         // ----- fMacFontID -----
  394.         short fontNumber;
  395.         Str255 pascalFontName;
  396.         
  397.         fFontName.ExportPascal(pascalFontName);
  398.         ::GetFNum(pascalFontName, &fontNumber);
  399.         
  400.         if (fontNumber == systemFont)
  401.         {
  402.             // fontNum is set to sysFont if fontName is not found.
  403.             // We must check for the special case where the system
  404.             // font is indeed being retrieved, by comparing the
  405.             // name of the font with that of the system font.    
  406.             Str255 sysFontName;
  407.             ::GetFontName(systemFont, sysFontName);
  408.             if (!::EqualString(pascalFontName, sysFontName, FALSE, FALSE))
  409.             {
  410. //                // font was not found
  411. //                BR_THROW(BR_XFontNameNotFound(fFaceName));
  412.             }
  413.         }
  414.         fMacFontID = fontNumber;
  415.  
  416.         fMacValidCache = TRUE;
  417.     }
  418. }
  419.  
  420. #endif
  421.  
  422. //----------------------------------------------------------------------------------------
  423. //    FW_CFontRep::IsEqual
  424. //----------------------------------------------------------------------------------------
  425.  
  426. FW_Boolean FW_CFontRep::IsEqual(const FW_CGraphicCountedPtrRep* other) const
  427. {
  428.     if (other == this)
  429.         return TRUE;
  430.         
  431.     FW_CFontRep* fontRep = FW_DYNAMIC_CAST(FW_CFontRep, other);
  432.     if (fontRep)
  433.     {
  434.         return
  435.             fFontStyle == fontRep->fFontStyle &&
  436.             fFontSize == fontRep->fFontSize &&
  437.             fFontName == fontRep->fFontName;
  438.     }
  439.     
  440.     return FALSE;
  441. }
  442.  
  443. //----------------------------------------------------------------------------------------
  444. //    FW_CFontRep::Read
  445. //----------------------------------------------------------------------------------------
  446.  
  447. void* FW_CFontRep::Read(FW_CReadableStream& archive)
  448. {
  449.     FW_CFontRep* objectRep = new FW_CFontRep(archive);
  450.     return objectRep;
  451. }
  452.  
  453. //----------------------------------------------------------------------------------------
  454. //    FW_CFontRep::GetFontMetrics
  455. //----------------------------------------------------------------------------------------
  456.  
  457. void FW_CFontRep::GetFontMetrics(FW_CGraphicDevice* device, FW_SFontMetrics& fontMetrics) const
  458. {    
  459.     FW_CFontRep* self = (FW_CFontRep*)this;
  460.     FW_PFont tempFont(self);
  461.     device->SelectFont(tempFont, FALSE);    // FALSE means no scaling
  462.     
  463.     FW_SFontMetrics::PrivGetFontMetrics(device, fontMetrics);
  464. }
  465.